import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入echarts
import echarts from 'echarts'



import Router from 'vue-router'
// 百度地图
import BaiduMap from 'vue-baidu-map'

var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8234/api'
axios.defaults.withCredentials = true // 使前端也能带上cookie
Vue.prototype.$axios = axios
Vue.config.productionTip = false
Vue.prototype.$echarts = echarts

// 全屏滚动 vue-fullpage.js
/*import 'fullpage.js/vendors/scrolloverflow';
import VueFullpage from 'vue-fullpage.js'*/


Vue.use(ElementUI)

Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'FvnWQtitBryGp20SfxfIpPNG6RYeOlcn'
})

/*Vue.use(VueFullpage)*/

// 因为vue-router版本的升级导致路由跳转失败
// 解决路由跳转时Navigation cancelled from "" to "" with a new navigation.出现的异常
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
    // 只有当存在用户信息或跳转的地址是以‘system’开头的路径才会渲染动态菜单;store.state = {systemMenus:{},username:''}
    if (store.state.user.username && to.path.startsWith('/system')) {
      getMenus(router, store)
      getCurrentRole(router, store)
    }
    if(store.state.user.username && to.path.startsWith('/customer')) {
      getCustomerMenus(router,store)
    }
    if(store.state.user.username && to.path.startsWith('/user_login')){
      next({
        name:'Customer'
      })
    }
    // 如果用户在已经登录了的情况下访问login页面，直接跳转到管理页面
    if (store.state.user.username && to.path.startsWith('/login')) {
      next({
        name: 'Dashboard'
      })
    }
    // 判断访问路径中是否有需要拦截的访问路径
    if (to.meta.requireAuth) {
      if (store.state.user.username) {
        axios.get("/authentication").then(resp => {
          if (resp) {
            next()
          }
        })

      } else {
        next({
          path: 'login',
          query: {redirect: to.fullPath}
        })
      }
    } else {
      next()
    }
  }
)

const getCurrentRole = (router, store) => {

  var date = store.state.user
  axios.post('/role/loginRole', date).then(resp => {
    if (resp && resp.status === 200 &&resp.data!==[]) {
      store.commit('getCurrentRole', resp.data)
    }
  })
}

const getMenus = (router, store) => {
  if (store.state.systemMenus.length > 0) {
    return;
  }
  axios.get('/permission/menu').then(resp => {
    if (resp && resp.status === 200) {
      var fmtRoutes = formatRoutes(resp.data.result)
      router.addRoutes(fmtRoutes)
      store.commit('getMenus', fmtRoutes)
    }
  })
}

const formatRoutes = (routes) => {
  let fmtRoutes = []
  routes.forEach(route => {
    if (route.children) {
      route.children = formatRoutes(route.children)
    }

    let fmtRoute = {
      path: route.url,
      component: resolve => {
        require(['./components/system/' + route.component + '.vue'], resolve)
      },
      name: route.name,
      nameZh: route.nameZh,
      iconCls: route.iconCls,
      meta: {
        requireAuth: true
      },
      children: route.children
    }
    fmtRoutes.push(fmtRoute)
  })
  return fmtRoutes
}

const getCustomerMenus = (router, store) => {
  if (store.state.customerMenus.length > 0) {
    return;
  }
  axios.get("/customer/menu").then(resp => {
    if (resp.data.code === 200) {
      var fmtRoutes = customerFormatRoutes(resp.data.result)
      router.addRoutes(fmtRoutes)
      store.commit('getCustomerMenus', fmtRoutes)
    }
  })
}

const customerFormatRoutes = (routes) => {
  let fmtRoutes = []
  routes.forEach(route => {
    if (route.children) {
      route.children = formatRoutes(route.children)
    }

    let fmtRoute = {
      path: route.url,
      component: resolve => {
        require(['./components/customer/reserve' + route.component + '.vue'], resolve)
      },
      name: route.name,
      nameZh: route.nameZh,
      iconCls: route.iconCls,
      meta: {
        requireAuth: true
      },
      children: route.children
    }
    fmtRoutes.push(fmtRoute)
  })
  return fmtRoutes
}

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App),
  router,
  store,
  components: {App},
  template: '<App/>'
})

